package com.project.XXXX.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.project.XXXX.controllerInterface.IPtjobMutual;
import com.project.XXXX.identification.tool.ValidateTool;
import com.project.XXXX.inter.tbl_dictMapper;
import com.project.XXXX.inter.tbl_divisionMapper;
import com.project.XXXX.inter.tbl_employeeinfoMapper;
import com.project.XXXX.inter.tbl_employerinfoMapper;
import com.project.XXXX.inter.tbl_jobenrollMapper;
import com.project.XXXX.inter.tbl_ptjobMapper;
import com.project.XXXX.inter.tbl_sysuserMapper;
import com.project.XXXX.inter.tbl_workplaceMapper;
import com.project.XXXX.inter.tbl_worktimeMapper;
import com.project.XXXX.model.tbl_employeeinfo;
import com.project.XXXX.model.tbl_employerinfo;
import com.project.XXXX.model.tbl_jobenroll;
import com.project.XXXX.model.tbl_ptjob;
import com.project.XXXX.model.tbl_ptjobWithBLOBs;
import com.project.XXXX.model.tbl_sysuser;
import com.project.XXXX.model.tbl_workplace;
import com.project.XXXX.model.tbl_worktime;
import com.project.XXXX.stringmodel.EEinfoAndjobString;
import com.project.XXXX.stringmodel.JobEnrollString;
import com.project.XXXX.stringmodel.PtJobForShown;
import com.project.XXXX.stringmodel.PtJobForShownWithBlobs;
import com.project.XXXX.tools.ProjectTools;

@Controller
public class PtjobMutualImpl implements IPtjobMutual {
	@Resource
	private tbl_ptjobMapper tbl_ptjobMapper;
	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;
	@Resource
	private tbl_worktimeMapper tbl_worktimeMapper;
	@Resource
	private tbl_dictMapper tbl_dictMapper;
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	@Resource
	private tbl_divisionMapper tbl_divisionMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_jobenrollMapper tbl_jobenrollMapper;
	@Resource
	private tbl_workplaceMapper tbl_workplaceMapper;

	@RequestMapping(value = "/showByTypeandPlace.do", method = RequestMethod.POST)
	void showByTypeandPlace(@RequestParam("page") int currentPage, @RequestParam("type") String type,
			@RequestParam("country") String country, HttpServletRequest request, HttpServletResponse response,
			Model model, HttpSession session) {
		int pageSize = 5;
		currentPage = (currentPage - 1) * pageSize;

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pagesize", pageSize);
		map.put("offset", currentPage);

		if (!StringUtils.isBlank(type)) {
			map.put("_pttypeid", type);
		}

		if (!StringUtils.isBlank(country)) {
			if (StringUtils.endsWith(country, "00"))
				country = country.substring(0, 4) + "%";
			else
				country = country + "%";
			map.put("_cityid", country);
		}
		List<tbl_ptjobWithBLOBs> list = tbl_ptjobMapper.showByTypeandPlace(map);
		// System.out.println(list.size());
		List<PtJobForShown> l = new ArrayList<PtJobForShown>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String pttypeid;
		String cityid;
		String salaryunit;
		String billcycleid;
		for (tbl_ptjobWithBLOBs job : list) {

			cityid = job.getCityid() + "";
			cityid = tbl_divisionMapper.selectCountryName(cityid).getDiming();
			salaryunit = tbl_dictMapper.selectByPrimaryKey(job.getSalaryunit()).getValue();
			pttypeid = tbl_dictMapper.selectByPrimaryKey(job.getPttypeid()).getValue();
			billcycleid = tbl_dictMapper.selectByPrimaryKey(job.getBillcycleid()).getValue();
			String Genderne = tbl_dictMapper.selectByPrimaryKey(job.getGenderneedid()).getValue();

			tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(job.getMobile());
			int applyed, confirmed;
			map.put("ptjobid", job.getId());
			map.put("status", 30);
			applyed = tbl_jobenrollMapper.getReqJobUserCount(map);
			map.clear();
			map.put("ptjobid", job.getId());
			map.put("status", 31);
			confirmed = tbl_jobenrollMapper.getReqJobUserCount(map);
			PtJobForShown ptjob = new PtJobForShown(job.getId() + "",

					job.getMobile(), job.getTitle(), pttypeid, cityid, sdf.format(job.getBegindate()),
					sdf.format(job.getEnddate()), job.getPersonnum() + "", Genderne, job.getSalary() + "", salaryunit,
					billcycleid, sdf.format(job.getCreatetime()), job.getInterview() + "", job.getTrain() + "",
					job.getTimebegin() + "", job.getTimeend(), tbl_employerinfo.getLogo(), applyed, confirmed);
			l.add(ptjob);
		}

		String json;

		Gson gson = new Gson();
		json = gson.toJson(l);

		ProjectTools.ResponseOut(response, json);

	}

	@RequestMapping(value = "/showByTypeandPlaceCount.do", method = RequestMethod.POST)
	void showByTypeandPlaceCount(@RequestParam("type") String type, @RequestParam("country") String country,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(type)) {
			map.put("_pttypeid", type);

		}
		if (!StringUtils.isBlank(country)) {

			if (StringUtils.endsWith(country, "00"))
				country = country.substring(0, 4) + "%";
			else
				country = country + "%";
			map.put("_cityid", country);
		}
		int size = tbl_ptjobMapper.showByTypeandPlaceCount(map);
		ProjectTools.ResponseOut(response, "{\"CountNums\":\"" + size + "\"}");

	}

	@RequestMapping(value = "/showItemDetail.do", method = RequestMethod.POST)
	String showItemDetail(@RequestParam("ptid") int ptid, HttpServletRequest request, HttpServletResponse response,
			Model model, HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");

		tbl_ptjobWithBLOBs job = tbl_ptjobMapper.selectByPrimaryKey(ptid);
		if (job != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String pttypeid;
			String cityid;
			String salaryunit;
			String billcycleid;
			String sex;
			String times = "";
			String places = "";
			List<tbl_workplace> lplace = tbl_workplaceMapper.selectJobId(job.getId());
			for (tbl_workplace p : lplace) {
				places += p.getJobplace() + "%";
			}
			places = places.substring(0, places.length() - 1);
			Map<String, Object> map = new HashMap<String, Object>();
			cityid = job.getCityid() + "";
			cityid = tbl_divisionMapper.selectCountryName(cityid).getDiming();
			salaryunit = tbl_dictMapper.selectByPrimaryKey(job.getSalaryunit()).getValue();
			pttypeid = tbl_dictMapper.selectByPrimaryKey(job.getPttypeid()).getValue();
			billcycleid = tbl_dictMapper.selectByPrimaryKey(job.getBillcycleid()).getValue();
			sex = tbl_dictMapper.selectByPrimaryKey(job.getGenderneedid()).getValue();
			String Genderne = tbl_dictMapper.selectByPrimaryKey(job.getGenderneedid()).getValue();
			tbl_employerinfo tbl_employerinfo = tbl_employerinfoMapper.selectByPrimaryKey(job.getMobile());
			int applyed, confirmed;
			map.put("ptjobid", job.getId());
			map.put("status", 30);
			applyed = tbl_jobenrollMapper.getReqJobUserCount(map);
			map.clear();
			map.put("ptjobid", job.getId());
			map.put("status", 31);
			confirmed = tbl_jobenrollMapper.getReqJobUserCount(map);
			PtJobForShownWithBlobs ptjob = new PtJobForShownWithBlobs(job.getId() + "", job.getMobile(), job.getTitle(),
					pttypeid, cityid, sdf.format(job.getBegindate()), sdf.format(job.getEnddate()),
					job.getPersonnum() + "", Genderne, job.getSalary() + "", salaryunit, billcycleid,
					sdf.format(job.getCreatetime()), job.getInterview() + "", job.getTrain() + "",
					job.getTimebegin() + "", job.getTimeend(), tbl_employerinfo.getLogo(), job.getRequirement(),
					job.getContect(), places, applyed, confirmed);
			request.setAttribute("ptjobInfo", ptjob);
			tbl_employerinfo s = tbl_employerinfoMapper.selectByPrimaryKey(job.getCreatorid());
			request.setAttribute("ERInfo", s);
			return "PtJobdetail";
		}
		return "job";

	}

	public boolean canRequest(tbl_employeeinfo info) {
		if (info != null) {
			if(StringUtils.isAnyEmpty(info.getUsername(),info.getGender(),info.getSchool())||info.getBirthday() ==null)
	
				return false;
			else
			return true;
		}
		return false;
	}

	@RequestMapping(value = "/commitJobRequest.do", method = RequestMethod.POST)
	void commitJobRequest(@RequestParam("ptjobid") int ptjobid, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		tbl_employeeinfo info = (tbl_employeeinfo) session.getAttribute("EEinfo");
		String j;
		if (canRequest(info) == false) {
			map.put("state", "error");
			map.put("error_description", "please complete your information");
			map.put("hint", "请完善你的信息，才可以报名");
			
		} else {
			tbl_jobenroll record = null;
			map.put("mobile", tbl_sysuser.getMobile());
			map.put("ptjobid", ptjobid);
			
			tbl_jobenroll t = tbl_jobenrollMapper.selectByPtidandmobile(map);
			map.clear();
			if (t == null) {
				record = new tbl_jobenroll();
				record.setPtjobid(ptjobid);
				record.setHappendate(new Date());
				record.setStateid(30);
				record.setMobile(tbl_sysuser.getMobile());
				tbl_jobenrollMapper.insert(record);
			
				map.put("state", "success");
				map.put("success_description", "commit_success");
				map.put("hint", "报名成功");
			} else {
				map.put("state", "error");
				map.put("error_description", "have been commited");
				map.put("hint", "已经报过名了");
			}

			
		}

		j = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, j);
	}

	@RequestMapping("/getAllEECount.do")
	public void getAllEECount(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		int size, size1, size2 = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", tbl_sysuser.getMobile());
		map.put("status", 30);
		size = tbl_jobenrollMapper.getDiffReqJobCount(map);
		map.put("status", 31);
		size1 = tbl_jobenrollMapper.getDiffReqJobCount(map);
		map.put("status", 35);
		size2 = tbl_jobenrollMapper.getDiffReqJobCount(map);
		map.clear();
		map.put("nocheck", size);
		map.put("checkedin", size1);
		map.put("finish", size2);
		ProjectTools.ResponseOut(response, new Gson().toJson(map));

	}

	@RequestMapping("/getAllERCount.do")
	public void getAllERCount(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");

		int size, size1, size2, size3 = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("creatorid", tbl_sysuser.getMobile());
		map.put("status", 27);
		size = tbl_ptjobMapper.getDiffStautsPtJobSize(map);
		map.put("status", 28);
		size1 = tbl_ptjobMapper.getDiffStautsPtJobSize(map);
		map.put("status", 29);
		size2 = tbl_ptjobMapper.getDiffStautsPtJobSize(map);
		map.put("status", 40);
		size3 = tbl_ptjobMapper.getDiffStautsPtJobSize(map);
		map.clear();
		map.put("nostart", size);
		map.put("started", size1);
		map.put("finish", size2);
		map.put("end", size3);
		ProjectTools.ResponseOut(response, new Gson().toJson(map));
	}

	@RequestMapping(value = "/getDiffReqJob.do", method = RequestMethod.POST)
	public void getDiffReqJob(@RequestParam("page") int page, @RequestParam("status") int status,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		int pageSize = 10;
		int currentPage = page;
		currentPage = (currentPage - 1) * pageSize;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", tbl_sysuser.getMobile());
		map.put("status", status);
		map.put("pagesize", pageSize);
		map.put("offset", currentPage);
		List<tbl_jobenroll> listJob = tbl_jobenrollMapper.getDiffReqJob(map);
		List<JobEnrollString> l = new ArrayList<JobEnrollString>();
		for (tbl_jobenroll job : listJob) {

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

			String salaryunit;

			tbl_ptjob tbl_ptjob = tbl_ptjobMapper.selectByPrimaryKey(job.getPtjobid());
			String time = sdf.format(tbl_ptjob.getBegindate()) + "~" + sdf.format(tbl_ptjob.getEnddate());
			salaryunit = tbl_dictMapper.selectByPrimaryKey(tbl_ptjob.getSalaryunit()).getValue();

			JobEnrollString jb = new JobEnrollString(job.getId() + "", sdf.format(job.getHappendate()),
					tbl_ptjob.getTitle(), tbl_ptjob.getSalary() + "", salaryunit, job.getPtjobid() + "",
					job.getMobile(), time);
			l.add(jb);
		}

		String json;

		Gson gson = new Gson();
		json = gson.toJson(l);
		ProjectTools.ResponseOut(response, json);

	}

	@RequestMapping(value = "/getDiffReqJobCount.do", method = RequestMethod.POST)
	public void getDiffReqJobCount(@RequestParam("status") int status, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		int size = 0;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("mobile", tbl_sysuser.getMobile());
		map.put("status", status);
		size = tbl_jobenrollMapper.getDiffReqJobCount(map);
		ProjectTools.ResponseOut(response, "{\"CountNums\":\"" + size + "\"}");

	}

	@RequestMapping(value = "/changeDiffReqJob.do", method = RequestMethod.POST)
	public void changeJobStatus(@RequestParam("status") int status, @RequestParam("reqid") int reqid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		String temp = "{\"state\":\"error\"}";
		tbl_jobenroll record = tbl_jobenrollMapper.selectByPrimaryKey(reqid);

		if (record != null) {
			int ptid = record.getPtjobid();
			if (ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, ptid)
					|| ValidateTool.IsJobEnroll(tbl_sysuser, tbl_jobenrollMapper, ptid))
				if (record.getStateid() != status) {
					record.setStateid(status);
					record.setId(reqid);
					tbl_jobenrollMapper.updateByPrimaryKeySelective(record);
					temp = "{\"state\":\"success\"}";
				}
		}

		ProjectTools.ResponseOut(response, temp);
	}

	@RequestMapping(value = "/getReqUser.do", method = RequestMethod.POST)
	public void getReqUser(@RequestParam("page") int page, @RequestParam("ptjobid") int ptjobid,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		int pageSize = 10;
		int currentPage = page;
		currentPage = (currentPage - 1) * pageSize;
		tbl_ptjob t = tbl_ptjobMapper.selectByPrimaryKey(ptjobid);
		if (t != null) {
			if (ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, ptjobid)) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ptjobid", ptjobid);
				map.put("pagesize", pageSize);
				String status = request.getParameter("status");
				map.put("status", status);
				map.put("offset", currentPage);
				List<tbl_jobenroll> listJob = tbl_jobenrollMapper.getReqJobUser(map);
				List<EEinfoAndjobString> l = new ArrayList<EEinfoAndjobString>();
				for (tbl_jobenroll job : listJob) {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					tbl_employeeinfo info = tbl_employeeinfoMapper.selectByPrimaryKey(job.getMobile());
					String gender;
				if(info.getGender().equals("13"))
					gender="男";
					else
						gender="女";
					int age = 0;
					if (info.getBirthday() != null)
						age = (new Date().getYear()) - info.getBirthday().getYear();
					EEinfoAndjobString e = new EEinfoAndjobString(info.getRealname(), info.getUsername(),
							info.getMobile(), info.getMajor(), info.getEmail(), gender, info.getSchool(),
							info.getTellus(), info.getTheyear(), info.getWechatid(), sdf.format(job.getHappendate()),
							age + "", job.getId() + "", job.getStateid() + "");
					l.add(e);
				}
				String json;
				Gson gson = new Gson();
				json = gson.toJson(l);
				ProjectTools.ResponseOut(response, json);
			}

		}

	}

	@RequestMapping(value = "/getReqUserCount.do", method = RequestMethod.POST)
	public void getReqUserCount(@RequestParam("ptjobid") int ptjobid, @RequestParam("status") int status,
			HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {

		tbl_sysuser tbl_sysuser = (tbl_sysuser) session.getAttribute("User");
		tbl_ptjob t = tbl_ptjobMapper.selectByPrimaryKey(ptjobid);
		if (t != null) {
			if (ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, ptjobid)) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ptjobid", ptjobid);
				map.put("status", status);
				int size = 0;
				size = tbl_jobenrollMapper.getReqJobUserCount(map);
				ProjectTools.ResponseOut(response, "{\"CountNums\":\"" + size + "\"}");
			}

		}
	}

}
